package com.faxsun.common.currency.service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.broadleafcommerce.common.currency.domain.BroadleafCurrency;
import org.broadleafcommerce.common.money.Money;
import org.broadleafcommerce.core.catalog.domain.ProductOptionValueImpl;
import org.broadleafcommerce.core.catalog.domain.Sku;
import org.broadleafcommerce.core.catalog.domain.SkuBundleItem;
import org.broadleafcommerce.core.catalog.service.dynamic.DynamicSkuPrices;
import org.broadleafcommerce.core.catalog.service.dynamic.DynamicSkuPricingService;
import org.springframework.stereotype.Service;

import com.faxsun.common.currency.dao.FSCurrencyRateDao;
import com.faxsun.common.currency.domain.FSCurrencyEnum;
import com.faxsun.common.currency.domain.FSCurrencyRate;
import com.faxsun.core.catalog.domain.FSSku;

/**
 * 根据汇率计算人民币价格
 * 
 * @author zhaoteng.song@faxsun.com
 *
 */
@Service("currencySkuPricingService")
public class CurrencyRateDynamicSkuPricingServiceImpl implements DynamicSkuPricingService {

	private static final Log LOG = LogFactory.getLog(CurrencyRateDynamicSkuPricingServiceImpl.class);

	@Resource(name = "fsCurrencyRateDao")
	protected FSCurrencyRateDao currencyRateDao;

	@SuppressWarnings("rawtypes")
	@Override
	public DynamicSkuPrices getSkuPrices(Sku sku, HashMap skuPricingConsiderations) {

		return convertForSKU(sku);
	}

	@SuppressWarnings("rawtypes")
	@Override
	public DynamicSkuPrices getSkuBundleItemPrice(SkuBundleItem sku, HashMap skuPricingConsiderations) {
		// no change only return prices

		return convertForSKU(sku.getSku());
	}

	@SuppressWarnings("rawtypes")
	@Override
	public DynamicSkuPrices getPriceAdjustment(ProductOptionValueImpl productOptionValueImpl,
			Money priceAdjustment, HashMap skuPricingConsiderationContext) {
		DynamicSkuPrices prices = new DynamicSkuPrices();
		prices.setRetailPrice(priceAdjustment);
		return prices;
	}

	/**
	 * 从数据库读取汇率，转换为人民币
	 * 
	 * @param sku
	 * @return
	 */
	private DynamicSkuPrices convertForSKU(Sku sku) {
		FSSku skuImpl = (FSSku) sku;

		BroadleafCurrency source = skuImpl.getPriceBroadleafCurrency();
		BroadleafCurrency dest = FSCurrencyEnum.parse("CNY");
		DynamicSkuPrices prices = new DynamicSkuPrices();
		try {

			FSCurrencyRate rate = currencyRateDao.readByCodes(source, dest);
			BigDecimal foreignPriceBD = skuImpl.getForeignPrice().getAmount();
			BigDecimal retailPriceBD = BigDecimal.ZERO;

			if (rate != null) {
				retailPriceBD = foreignPriceBD.multiply(rate.getRate());

			} else {
				if (foreignPriceBD != null && !foreignPriceBD.equals(BigDecimal.ZERO)) {
					retailPriceBD = foreignPriceBD;
				} else {
					retailPriceBD = skuImpl.getOriginRetailPrice().getAmount();
				}
			}
			
			//将小数向上取整
			Money roundedPrice = dest.equals(source)? new Money(retailPriceBD):
				new Money(retailPriceBD.setScale(0, RoundingMode.CEILING));

			prices.setRetailPrice(roundedPrice);

		} catch (Exception e) {
			// any exception
			LOG.error("Convert SKU Foreign Price ERROR");
			LOG.error(e.getLocalizedMessage());
			// 设置为原始价格
			prices.setRetailPrice(skuImpl.getOriginRetailPrice());
		}
		return prices;
	}
}
